package com.keba.lamp.complaint.strategy.impl;

import com.keba.lamp.common.constant.AppendixType;
import com.keba.lamp.complaint.enumeration.MimeEnum;
import com.keba.lamp.complaint.strategy.AbstractCaseFileStrategy;
import com.keba.lamp.complaint.strategy.domain.CaseDownloadParam;
import com.keba.lamp.complaint.strategy.domain.CaseDownloadResult;
import com.keba.lamp.complaint.vo.result.CaseResultVO;
import com.keba.lamp.file.entity.File;
import com.keba.lamp.file.vo.param.FileUploadVO;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;

/**
 * 起诉状下载
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Getter
@Setter
public class CasePdfStrategy extends AbstractCaseFileStrategy {

    @Override
    public CaseDownloadResult exec(CaseDownloadParam param) throws Exception {
        CaseResultVO caseResultVO = caseService.getCaseById(param.getCaseId());
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        pdfCommonService.createCasePdf(caseResultVO, outputStream, param.getFileType());

        FileUploadVO fileUploadVO = new FileUploadVO();
        fileUploadVO.setBizType(AppendixType.Download.DOWNLOAD__CASE__PDF__FILE);
        fileUploadVO.setContentType(MimeEnum.PDF.getContentType());
        String caseName = StringUtils.isBlank(caseResultVO.getCaseName()) ? "" : caseResultVO.getCaseName();
        String fileName = caseName + "起诉书." + param.getFileType();
        File file = fileContext.upload(outputStream.toByteArray(), fileName, fileUploadVO);
        return CaseDownloadResult.builder().url(file.getUrl()).build();
    }
}
